package br.com.puc.repositories;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;

import br.com.puc.models.Entity;

public class HibernateUtil {
	public Session session;

	public HibernateUtil(Session session) {
		this.session = session;
	}
	
	public <T extends Entity> long getQuantidadeRegistros (Class<T> classeEntidade) {
		Criteria criteria = session.createCriteria(classeEntidade)
									.setProjection(Projections.rowCount());
		
		return ((Long)criteria.list().get(0)).longValue();
	}

	public <T extends Entity> long getQuantidadeRegistros (Class<T> classeEntidade, Criterion criterion) {
		Criteria criteria = session.createCriteria(classeEntidade)
									.add(criterion)
									.setProjection(Projections.rowCount());
		
		return ((Long)criteria.list().get(0)).longValue();
	}
}